package com.yonyou.pmclouds.repairlog.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.auditlog.entity.AuditLogVO;
import com.yonyou.pmclouds.auditlog.service.IAuditLogQuery;
import com.yonyou.pmclouds.basecom.annotation.Lock;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.picture.entity.PictureParamVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.repairlog.entity.RepairLogVO;
import com.yonyou.pmclouds.repairlog.mapper.RepairLogMapper;
import com.yonyou.pmclouds.repairlog.service.rmiitf.RepairLogService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass=RepairLogService.class)
public class RepairLogServiceImpl implements RepairLogService{
    @Autowired
    private RepairLogMapper mapper;
    @Autowired
    private PictureRemoteService picService;
    @Autowired
    private IAuditLogQuery auditLogQuery;
    
    @Override
    public RepairLogVO add(RepairLogVO vo) throws BusinessException {
        if(vo == null) {
            return null;
        }
       SingleInsertBpTemplate<RepairLogVO> bp = new SingleInsertBpTemplate<RepairLogVO>(mapper);
       bp.insert(vo);
       // 图片保存
       PictureParamVO paramVO = new PictureParamVO(vo.getPk_problemlog(), vo.getPk_project(),BillTypeConst.PROBLEMLOG_BILLTYPE );
       picService.insertPictureForPub(vo, paramVO);
       
       return vo;
    }

    @Override
    public RepairLogVO[] query(String pk_problemlog) throws BusinessException {
        if(pk_problemlog == null) {
            return null;
        }

        RepairLogVO[] repairLogVOs = mapper.queryByPkproblemlog(pk_problemlog);
        Map<String, List<String>> picsMap = queryPics(repairLogVOs);
        // 查询验收记录
        Map<String, AuditLogVO> auditLogVOMap = auditLogQuery.getByPkBill(pk_problemlog);

        String pkRepair;
        for (RepairLogVO repairLogVO : repairLogVOs) {
            pkRepair = repairLogVO.getPk_repair();
            repairLogVO.setAuditLogVO(auditLogVOMap.get(pkRepair));
            List<String> list = picsMap.get(pkRepair);
            if(list == null || list.size() == 0) {
                continue;
            }
            repairLogVO.setPictures(list.toArray(new String[0]));
        }

        return repairLogVOs;
    }

    @Override
    @Lock
    public RepairLogVO update(RepairLogVO vo,String[] fields) throws BusinessException {
        SingleUpdateBpTemplate<RepairLogVO> bp = new SingleUpdateBpTemplate<RepairLogVO>(mapper, fields);
        return bp.update(vo);
    }
    
    private Map<String,List<String>> queryPics(RepairLogVO[] repairLogVOs) throws BusinessException {
        if(ArrayUtils.isEmpty(repairLogVOs)) {
            return new HashMap<>();
        }
        List<String> pklist = new ArrayList<String>();
        for (RepairLogVO repairLogVO : repairLogVOs) {
            pklist.add(repairLogVO.getPk_repair());
        }
        return picService.queryPicsByPkEntitys(pklist.toArray(new String[pklist.size()]));
    }

    @Override
    public RepairLogVO queryOne(String pk_repair) throws BusinessException {
        return mapper.getById(pk_repair);
    }
}
